package net.jeeshop.web.action.front.bbs;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.google.common.collect.Maps;
import net.jeeshop.core.Services;
import net.jeeshop.core.front.SystemManager;
import net.jeeshop.services.front.account.bean.Account;
import net.jeeshop.services.front.bbs.BbsCommentService;
import net.jeeshop.services.front.bbs.BbsDetailService;
import net.jeeshop.services.front.bbs.bean.BbsComment;
import net.jeeshop.services.front.bbs.bean.BbsDetail;
import net.jeeshop.services.front.bbs.bean.BbsImg;
import net.jeeshop.services.front.bbs.dao.BbsDetailDao;
import net.jeeshop.services.front.bbs.dto.BbsCommentDTO;
import net.jeeshop.services.front.bbs.dto.BbsDetailDTO;
import net.jeeshop.web.action.front.FrontBaseController;
import net.jeeshop.web.util.LoginUserHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zuohao on 16/7/7.
 */
@Controller("frontBbsDetailAction")
@RequestMapping("bbsDetail")
public class BbsDetailAction extends FrontBaseController<BbsDetail> {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BbsDetailAction.class);

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private BbsDetailService bbsDetailService;

    @Autowired
    private BbsCommentService bbsCommentService;

    @Override
    public Services<BbsDetail> getService() {
        return bbsDetailService;
    }

    @RequestMapping("selectBbsDetailList")
    @ResponseBody
    public Map<String, Object> selectBbsDetailList(@RequestParam("categoryCode") String categoryCode, @RequestParam("offset") Integer offset, @RequestParam("rows") Integer rows) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("categoryCode", categoryCode);
        map.put("offset", offset);
        map.put("rows", rows);
        Map<String, Object> result = new HashMap<String, Object>();

        List<BbsDetailDTO> selectBbsDetailList = bbsDetailService.selectBbsDetailList(map);
        if (!CollectionUtils.isEmpty(selectBbsDetailList)) {
            for (BbsDetailDTO bbsDetail : selectBbsDetailList) {
                List<BbsImg> bbsImgs = bbsDetailService.selectBbsImage(bbsDetail.getId());
                if (!CollectionUtils.isEmpty(bbsImgs)) {
                    for (BbsImg bbsImg : bbsImgs) {
                        if (StringUtils.isNotBlank(bbsImg.getUrl())) {
                            bbsImg.setUrl(SystemManager.getInstance().getSystemSetting().getImageRootPath() + bbsImg.getUrl());
                        }
                    }
                }
                bbsDetail.setBbsImgList(bbsImgs);
                bbsDetail.setHeadImage(systemManager.getSystemSetting().getImageRootPath() + bbsDetail.getHeadImage());
            }
        }
        result.put("bbsDetailList", selectBbsDetailList);
        result.put("count", bbsDetailService.selectBbsDetailCountList(map));
        return result;
    }

    @RequestMapping("insertBbsDetail")
    @ResponseBody
    public String insertBbsDetail(@RequestParam("content") String content,
                                  @RequestParam("categoryCode") String categoryCode, @RequestParam("imageLists") String imageLists, @RequestParam("createId") String createId) {

        BbsDetail bbsDetail = new BbsDetail();
        bbsDetail.setContent(content);
        bbsDetail.setCategoryCode(categoryCode);
        bbsDetail.setCreateId(createId);
        bbsDetail.setStatus("y");
        bbsDetail.setCreateTime(sdf.format(System.currentTimeMillis()));
        bbsDetailService.insertBbsDetail(bbsDetail);
        if (StringUtils.isNotBlank(imageLists)) {
            for (String img : imageLists.split(",")) {
                if (StringUtils.isNotBlank(img)) {
                    BbsImg bbsImg = new BbsImg();
                    bbsImg.setBbsDetailId(bbsDetail.getId());
                    bbsImg.setUrl(img);
                    bbsImg.setStatus("y");
                    bbsImg.setSort(0);
                    bbsDetailService.insertBbsImage(bbsImg);
                }
            }
        }


        return "true";
    }


    @RequestMapping("selectBbsDetail")
    @ResponseBody
    public BbsDetailDTO selectBbsDetailList(BbsDetail bbsDetail) {

        BbsDetailDTO bbsDetailDTO = bbsDetailService.selectBbsDetail(bbsDetail);
        if (bbsDetailDTO != null) {
            List<BbsImg> bbsImgs = bbsDetailService.selectBbsImage(bbsDetail.getId());
            if (!CollectionUtils.isEmpty(bbsImgs)) {
                for (BbsImg bbsImg : bbsImgs) {
                    if (StringUtils.isNotBlank(bbsImg.getUrl())) {
                        bbsImg.setUrl(SystemManager.getInstance().getSystemSetting().getImageRootPath() + bbsImg.getUrl());
                    }
                }
            }
            bbsDetailDTO.setBbsImgList(bbsImgs);
            bbsDetailDTO.setHeadImage(systemManager.getSystemSetting().getImageRootPath() + bbsDetailDTO.getHeadImage());

//            BbsComment bbsComment = new BbsComment();
//            bbsComment.setBbsDetailId(bbsDetailDTO.getId());
//            bbsComment.setType("2");
            Map<String, Object> param = Maps.newHashMap();
            param.put("type", "2");
            param.put("bbsDetailId", bbsDetailDTO.getId());
            List<BbsCommentDTO> bbsComments = bbsCommentService.selectBbsCommentDTOByBbsDetailId(param);
            if (!CollectionUtils.isEmpty(bbsComments)) {
                for (BbsCommentDTO bbsComment : bbsComments) {
                    if (StringUtils.isNotBlank(bbsComment.getHeadImage())) {
                        bbsComment.setHeadImage(SystemManager.getInstance().getSystemSetting().getImageRootPath() + bbsComment.getHeadImage());
                    }
                }
                bbsDetailDTO.setBbsCommentDTOs(bbsComments);
            }

        }

        return null;

    }


}
